Skip to content

fix: size a blob whose content is a complete replacement from the replacement - #288

Closed
PabloPardoGarcia wants to merge 2 commits into
mainfrom
pablo/rius-1875-strip-marker-byte-size
Closed

PabloPardoGarcia wants to merge 2 commits into
mainfrom
pablo/rius-1875-strip-marker-byte-size

Conversation

@PabloPardoGarcia

@PabloPardoGarcia PabloPardoGarcia commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

This implements the rius-semconv rule from glassflow/rius-semconv#115. A native blob part whose content is a complete replacement takes its byte_size from the replacement. The size is omitted when it is unknown or larger than 2^53 - 1. The part's valid mime_type comes first, and the replacement's mime fills in when the part has none.

Why it matters here

The SDK's serializer strips a blob's base64 content to a replacement ([stripped image/png 8 bytes]) before the exporter rebuilds the part. So any blob set through the SDK's own setters lost its byte_size, even when it was sent as base64. Bytes, which become [stripped unknown 8 bytes], lost it too. Now both store the same part.

Changes

Long digit runs

int() refuses a string over 4300 digits, leading zeros included, and is_replacement now parses the size. The strip calls it on sibling strings inside the export chain, so a replacement with a long digit run in a message, an output or a mime key raised ValueError and dropped the whole batch. parse_replacement now drops leading zeros first and treats more than 16 significant digits as an unknown size without converting them, the same as the reference.

tests/test_byte_objects.py sends three such attributes through init's real export chain (a blob's content, a mime_type sibling, an Anthropic-style data) and checks each span is exported, plus a blob of 5000 zeros then 8 stored with byte_size: 8. All four failed before the fix (span export failed: the export chain raised ValueError).

Checks

  • ruff check, ruff format --check, mypy and pytest pass (2927 passed, 22 skipped, 1 xfailed).
  • Without the _media.py change, the contract examples and the end-to-end test fail.

Merge note: squash-merge glassflow/rius-semconv#115 first. Then re-sync this branch from semconv main (scripts/sync-semconv-fixtures.sh). Only SOURCE and the vendored headers should change.

Related: RIUS-1875

…lacement

A native blob part whose content is a complete replacement now takes
byte_size from it, and its mime when the part has no valid mime_type, per the
rius-semconv media contract. The serializer strips a blob's base64 content to
a replacement before the exporter rebuilds the part, so a blob set through
the SDK's own setters lost its byte_size; bytes and base64 now store alike.
@PabloPardoGarcia

Copy link
Copy Markdown
Member Author

Automated review

Summary. parse_replacement and the native_part branch match the reference and the other implementations. I ran a 2,760-case differential corpus of native blob parts (mime token edge cases, digit/sign/unicode/exponent sizes, the 2^53 bounds, int64 overflow, near misses and replacements inside text) through this SDK, the reference, the TypeScript SDK and the sink, and got identical output on every case. Mime precedence (the part's own valid mime_type, then the replacement's) and modality (blob_part takes the mime's modality before own) are right. The new contract examples drive test_a_contract_part_example_is_made_byte_for_byte, test_a_contract_native_example_is_rebuilt_byte_for_byte_on_the_native_path and the in-place test, and the coverage check keeps CONTRACT_EXAMPLE_INPUTS in sync. The vendored media_parts.yaml is byte-identical to rius-semconv d270c655. uv run pytest tests gives 2917 passed, 22 skipped and 1 xfailed.

One blocker.

Blocker: a long digit run in a replacement raises inside the exporter and drops the batch

src/rius/_media.py:255 (int(size)), reached from is_replacement at :264

Since Python 3.11, int() refuses strings longer than 4300 digits, and leading zeros count toward that limit. is_replacement used to be a pure check. Now it converts the size, and the strip calls it on context strings (_sibling_string at :314, and :832). So a string that comes from model or tool output can raise ValueError inside the export chain:

v = "[stripped unknown " + "9" * 4301 + " bytes]"
span.set_attribute("gen_ai.input.messages", json.dumps([{"role": "user", "parts": [{"type": "blob", "content": v}]}]))  # dropped
span.set_attribute("output", json.dumps({"mime_type": v, "data": "QUJD"}))                                            # dropped
span.set_attribute("output", json.dumps({"type": "image", "source_type": "base64", "data": v}))                      # dropped

Every one of these logs span export failed: the export chain raised ValueError: Exceeds the limit (4300 digits) ... traces are not being delivered, and no span from that export reaches the exporter. Later failures are logged only at DEBUG. On main the same inputs export. The contract also says leading zeros are allowed, so "[stripped unknown " + "0" * 5000 + "8 bytes]" should give byte_size 8. TypeScript and Go already do.

Suggestion:

if digits:
    significant = size.lstrip("0") or "0"
    byte_size = int(significant) if len(significant) <= 16 else None  # 2^53 - 1 has 16 digits

Add a test that sets these three attributes and asserts the spans export, plus one for the 5000-zeros case giving 8. The reference generator has the same line (rius-semconv#115), so the fix there and here should land together.

Suggestion: a blob set as an object and as a JSON string can still store differently

tests/test_byte_objects.py:524 covers mime_type with base64. Through this SDK's real path (serialize strips, then the exporter rebuilds), these still diverge between object and string form:

blob as object as JSON string
{"mimeType":"image/png","content":"QUJD"} image, image/png, 3 document, 3
{"modality":"audio","content":"data:image/jpeg;base64,QUJD"} audio, no size image, image/jpeg, 3
{"mime_type":"image/png","content":"data:image/jpeg;base64,QUJD"} image, image/png, no size image, image/jpeg, 3

The data URL rows predate this PR. The cause is in the contract (rule (e) vs native_part), so I've written it up on rius-semconv#115. Nothing to change here unless the contract changes. I mention it because the PR's goal statement reads as covering these.

Nits

int() refuses a string over 4300 digits, leading zeros included, and
is_replacement now parses the size. The strip calls it on sibling
strings inside the export chain, so a replacement with a long digit
run in a message, an output or a mime key raised ValueError and the
whole batch was dropped.

Drop leading zeros first, and treat a run with more significant digits
than 2^53 - 1 has as an unknown size without converting it. Re-vendor
the contract, whose new examples pin 5000 zeros then 8 (byte_size 8),
5000 nines and a size past int64 (no byte_size).
@PabloPardoGarcia

Copy link
Copy Markdown
Member Author

Addressed in 4260f88.

  • Blocker: parse_replacement drops leading zeros first and treats more than 16 significant digits as an unknown size without calling int(), matching the reference fix in rius-semconv#115 (d826d04).
  • Tests: tests/test_byte_objects.py sends your three attributes (a blob's content, a mime_type sibling, an Anthropic-style data) through init's real export chain and checks each span is exported, plus a blob of 5000 zeros then 8 stored with byte_size: 8. All four failed before the fix with span export failed: the export chain raised ValueError.
  • The contract is re-vendored from d826d04, so its new examples (5000 zeros then 8, 5000 nines, a size past int64) run here too.
  • The object vs. string twins are narrowed in the contract text and listed as a follow-up on rius-semconv#115.
  • Re-vendoring from semconv main after feat: take the main agent identity into rius.main_agent.*, and version the agent at both scopes #115 squash-merges is still in the merge note.

ruff check, ruff format --check, mypy and pytest pass (2927 passed, 22 skipped, 1 xfailed).

@PabloPardoGarcia

Copy link
Copy Markdown
Member Author

Landed through #289, together with the rius-semconv v1.13.0 sync.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant